


function parseMd5Mesh(lines) {

    let info = {
        imagePath:'',

        numVerts:0,
        verts:[],

        numTris:0,
        tris:[],

        numWeights:0,
        weights:[],
    };

    let len = lines.length;
    for (let i = 0; i < len; ++i){
        let line = lines[i];
        let firstWord = getFirstWord(line.trim());
        switch (firstWord) {
            case "shader":
                // image name
                info.imagePath = getImagePath(line);
                break;
            case "numverts":
                info.numVerts = getNumVerts(line);
                break;
            case "vert":
                let vertInfo = getVertData(line);
                if (vertInfo){
                    info.verts.push(vertInfo);
                }
                break;
            case "numtris":
                info.numTris = getNumTris(line);
                break;
            case "tri":
                let triData = getTriData(line);
                if (triData){
                    info.tris.push(triData);
                }
                break;
            case "numweights":
                info.numWeights = getNumWeights(line);
                break;
            case "weight":
                let weightData = getWeightData(line);
                if (weightData){
                    info.weights.push(weightData);
                }
                break;
            default:
                //  ignore //
                break;

        }
    }
    return info;
}

function getFirstWord(line) {
    let contents = line.split(" ").filter(item=>item.length > 0);
    if (contents.length > 0){
        return contents[0]
    }
    return "";
}

function getImagePath(line) {
    let reg = /"(.*)"/g;
    let ret = reg.exec(line);
    if (ret.length > 1){
        return ret[1];
    }
    return ""
}

function getNumVerts(line) {
    let reg = /\d+/g;
    return parseInt(line.match(reg)[0]);
}

function getNumTris(line) {
    let reg = /\d+/g;
    return parseInt(line.match(reg)[0]);
}

function getNumWeights(line) {
    let reg = /\d+/g;
    return parseInt(line.match(reg)[0]);
}

function getVertData(line){
    let startIdx = line.indexOf('vert');
    let left = line.substring(startIdx + 4);
    let arr = left.split(" ").filter(item=>{
        return item.length > 0 && item != "(" && item != ")";
    });
    if (arr.length >= 5){
        return {
            vertexIdx:parseInt(arr[0]), // 顶点索引
            u:parseFloat(arr[1]),
            v:parseFloat(arr[2]),
            weightIdx:parseInt(arr[3]), // 第一个权重索引
            weightCount:parseInt(arr[4]), // 权重数量
        }
    } else{
        console.warn(`maybe parse line:${line} error!`)
        return null
    }
}

function getTriData(line) {
    let startIdx = line.indexOf('tri');
    let left = line.substring(startIdx + 3);
    let arr = left.split(" ").filter(item=>{
        return item.length > 0;
    });
    if (arr.length >= 4){
        return {
            triIdx:parseInt(arr[0]), // 三角形索引
            i0:parseInt(arr[1]), // 第一个顶点索引
            i1:parseInt(arr[2]),
            i2:parseInt(arr[3])
        }
    }else{
        console.warn(`maybe parse line:${line} error!`);
        return null
    }
}

function getWeightData(line) {
    let startIdx = line.indexOf('weight');
    let left = line.substring(startIdx + 6);
    let arr = left.split(" ").filter(item=>{
        return item.length > 0 && item != "(" && item != ")";
    });
    if (arr.length >= 6){
        return {
            weightIdx:parseInt(arr[0]), // 权重的索引
            boneIdx:parseInt(arr[1]), // 关联的骨骼
            weightValue:parseFloat(arr[2]), // 权重值
            offx:parseFloat(arr[3]),   // 权重平移的x分量
            offy:parseFloat(arr[4]),
            offz:parseFloat(arr[5]),
        }
    }else{
        console.warn(`maybe parse line:${line} error!`);
        return null
    }
}


export {parseMd5Mesh}
